package com.lzk.searchMasterMsg.controller;


import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.LogininfoContext;
import com.lzk.basic.util.PageList;
import com.lzk.pet.domain.SearchMasterMsg;
import com.lzk.pet.dto.AcceptDto;
import com.lzk.pet.query.SearchMasterMsgQuery;
import com.lzk.pet.service.ISearchMasterMsgService;
import com.lzk.user.domain.LoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/searchMasterMsg")

@Api(value = "寻主的API",description="寻主相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     * 待审核
     * @param searchMasterMsgQuery
     * @return
     */
    @ApiOperation(value = "待审核" )
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> Pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
            LoginInfo logininfo = LogininfoContext.getLogininfo(request);
            Long id = logininfo.getId();
            return searchMasterMsgService.Pending(searchMasterMsgQuery,id);
    }

    /**
     * 待处理
     * @param searchMasterMsgQuery
     * @return
     */
    @ApiOperation(value = "待处理" )
    @PostMapping("/handle")
    public PageList<SearchMasterMsg> handle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        LoginInfo logininfo = LogininfoContext.getLogininfo(request);
        Long id = logininfo.getId();
        return searchMasterMsgService.handle(searchMasterMsgQuery,id);
    }

    /**
     * 拒单接口
     * @param id
     * @return
     */
    @ApiOperation(value = "拒单" )
    @PostMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 接单接口
     * @param
     * @return
     */
    @ApiOperation(value = "接单" )
    @PostMapping("/accept")
    public JsonResult accept(@RequestBody AcceptDto dto){
        try {
            searchMasterMsgService.accept(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  JsonResult.me(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"接单失败");
        }
    }




    /**
     * 寻主接口
     * @param smm
     * @param request
     * @return
     */
    @ApiOperation(value = "发布寻主消息",notes = "")
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            LoginInfo logininfo = LogininfoContext.getLogininfo(request);
            Long id = logininfo.getId();
            searchMasterMsgService.publish(smm,id);
            return JsonResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me("发布失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙，请稍后重试");
        }

    }

    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id") Long id){
        return searchMasterMsgService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param searchMasterMsg
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if (searchMasterMsg.getId() == null){
                searchMasterMsgService.save(searchMasterMsg);
            }else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param searchMasterMsgQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        return searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            searchMasterMsgService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
